﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;
using SqlSugar;

namespace ZooKeeperSamples.Sugar
{
    public interface ISugerHandler : IDisposable
    {
        #region 事务
        /// <summary>
        /// 初始化事务 
        /// </summary>
        /// <param name="level"></param> 
        void BeginTran(IsolationLevel level = IsolationLevel.ReadCommitted);
        /// <summary> 
        /// 完成事务 
        /// </summary>
        void CommitTran();
        /// <summary> 
        /// 完成事务
        /// </summary> 
        void RollbackTran();
        #endregion

        #region 新增 
        /// <summary> 
        /// 新增 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="entity"> 实体对象 </param>
        /// <param name="isLock">是否加锁</param> 
        /// <returns>操作影响的行数</returns> 
        int Add<T>(T entity, bool isLock = false) where T : class, new();
        /// <summary> 
        /// 新增 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entitys">泛型集合</param> 
        /// <param name="isLock">是否加锁</param>
        /// <returns>操作影响的行数</returns>
        int Add<T>(List<T> entitys, bool isLock = false) where T : class, new();
        /// <summary> 
        /// 新增 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="isLock">是否加锁</param> 
        /// <returns>返回实体</returns>
        T AddReturnEntity<T>(T entity, bool isLock = false) where T : class, new();
        /// <summary> 
        /// 新增 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="isLock">是否加锁</param> 
        /// <returns>返回bool, 并将identity赋值到实体</returns> 
        bool AddReturnBool<T>(T entity, bool isLock = false) where T : class, new();
        /// <summary>
        /// 新增 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="entitys">泛型集合</param> 
        /// <param name="isLock">是否加锁</param> 
        /// <returns>返回bool, 并将identity赋值到实体</returns>
        bool AddReturnBool<T>(List<T> entitys, bool isLock = false) where T : class, new();
        #endregion

        #region 修改
        /// <summary>
        /// 修改数据源
        /// </summary>
        /// <typeparam name="T">泛型参数(集合成员的类型</typeparam>
        /// <returns>数据源</returns> 
        IUpdateable<T> Updateable<T>() where T : class, new();
        /// <summary>
        /// 修改（主键是更新条件）
        /// </summary>
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entity"> 实体对象 </param>
        /// <param name="isLock"> 是否加锁 </param>
        /// <returns>操作影响的行数</returns> 
        int Update<T>(T entity, bool isLock = false) where T : class, new();
        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
        /// <param name="update"> 字段集合 </param>
        /// <param name="where"> 条件 </param>
        /// <param name="isLock"> 是否加锁 </param>
        /// <returns>操作影响的行数</returns>
        int Update<T>(Expression<Func<T, T>> update, Expression<Func<T, bool>> where, bool isLock = false) where T : class, new();
        /// <summary>
        /// 修改（主键是更新条件）
        /// </summary>
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entitys">泛型集合</param>
        /// <param name="isLock"> 是否加锁 </param>
        /// <returns>操作影响的行数</returns> 
        int Update<T>(List<T> entitys, bool isLock = false) where T : class, new();
        #endregion

        #region 删除
        /// <summary> 
        /// 删除（主键是条件） 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="isLock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns>
        int Delete<T>(T entity, bool isLock = false) where T : class, new();
        /// <summary> 
        /// 删除（主键是条件） 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="where"> 条件 </param> 
        /// <param name="isLock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns> 
        int Delete<T>(Expression<Func<T, bool>> where, bool isLock = false) where T : class, new();
        #endregion

        #region 查询 
        /// <summary> 
        /// 查询数据源 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型</typeparam> 
        /// <returns>数据源</returns>

        ISugarQueryable<T> Queryable<T>() where T : class, new();
        /// <summary> 
        /// 查询 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="whereLambda">查询表达式</param> 
        /// <returns></returns>
        T Query<T>(Expression<Func<T, bool>> whereLambda) where T : class, new();
        /// <summary> 
        /// 查询集合 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="whereLambda">查询表达式</param> 
        /// <returns>实体</returns>
        List<T> QueryList<T>(Expression<Func<T, bool>> whereLambda) where T : class, new();
        /// <summary> 
        /// 查询集合 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="sql">sql</param> 
        /// <returns>实体</returns>
        List<T> QueryList<T>(string sql) where T : class, new();
        /// <summary> 
        /// 查询集合 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="whereLambda">查询表达式</param> 
        /// <returns>实体</returns>
        DataTable QueryDataTable<T>(Expression<Func<T, bool>> whereLambda) where T : class, new();
        /// <summary> 
        /// 查询集合 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="sql">sql</param> 
        /// <returns>实体</returns>
        DataTable QueryDataTable<T>(string sql) where T : class, new();
        /// <summary> 
        /// 分页查询 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="query">查询条件</param> 
        /// <param name="totalCount">总行数</param> 
        /// <returns>实体</returns>
        List<T> QueryPageList<T>(QueryDescriptor query, out int totalCount) where T : class, new();
        /// <summary> 
        /// 分页查询 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="query">查询条件</param> 
        /// <param name="totalCount">总行数</param> 
        /// <returns>实体</returns>
        DataTable QueryDataTablePageList<T>(QueryDescriptor query, out int totalCount) where T : class, new();
        /// <summary> 
        /// 查询集合 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam> 
        /// <param name="whereLambda">查询表达式</param> 
        /// <returns>Json</returns>
        string QueryJson<T>(Expression<Func<T, bool>> whereLambda) where T : class, new();
        /// <summary> 
        /// 查询存储过程 
        /// </summary> 
        /// <param name="procedureName">存储过程名称</param> 
        /// <param name="parameters">参数</param>
        DataTable QueryProcedure(string procedureName, List<SugarParameter> parameters);
        /// <summary> 
        /// 查询前多少条数据 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型</typeparam> 
        /// <param name="whereLambda">查询表达式</param> 
        /// <param name="num">数量</param> 
        /// <returns></returns>
        List<T> Take<T>(Expression<Func<T, bool>> whereLambda, int num) where T : class, new();
        /// <summary> 
        /// 查询单条数据 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型</typeparam> 
        /// <param name="whereLambda">查询表达式</param> 
        /// <returns></returns>
        T First<T>(Expression<Func<T, bool>> whereLambda) where T : class, new();
        /// <summary> 
        /// 是否存在 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型</typeparam> 
        /// <param name="whereLambda">查询表达式</param> 
        /// <returns></returns>
        bool IsExist<T>(Expression<Func<T, bool>> whereLambda) where T : class, new();
        /// <summary> 
        /// 合计 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型</typeparam> 
        /// <param name="field">字段</param> 
        /// <returns></returns>
        int Sum<T>(string field) where T : class, new();
        /// <summary> 
        /// 最大值 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型</typeparam> 
        /// <param name="field">字段</param> 
        /// <returns></returns>
        object Max<T>(string field) where T : class, new();
        /// <summary> 
        /// 最小值 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型</typeparam> 
        /// <param name="field">字段</param> 
        /// <returns></returns>
        object Min<T>(string field) where T : class, new();
        /// <summary> 
        /// 平均值 
        /// </summary> 
        /// <typeparam name="T">泛型参数(集合成员的类型</typeparam> 
        /// <param name="field">字段</param> 
        /// <returns></returns>
        int Avg<T>(string field) where T : class, new();
        #endregion



    }
}
